//
//  ViewController.m
//  DemoSecond
//
//  Created by yuzhuo on 16/7/19.
//  Copyright © 2016年 yuzhuo. All rights reserved.
//

#import "YZHomeController.h"
#import "Constant.h"
#import "HomeGraphCell.h"
#import "CellEnity.h"
#import "CellDetailsViewController.h"
#import "UIColor+Util.h"
#import "HomeGraphTopCell.h"
#import "DetailsController.h"
#import "PickupController.h"

@interface YZHomeController ()<UITableViewDataSource,UITableViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    UIImagePickerController *imagePickerController;
}
@property (nonatomic, strong) UITableView * tabView;
@property (nonatomic, strong) NSArray<CellEnity *> *arrayData;
/** topArray */
@property (nonatomic,strong) NSMutableArray<UIImage *> *topArray;


@end

@implementation YZHomeController

- (instancetype)init{
    
    self = [super init];
    if(self){
        self.tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
        
        self.tabView.dataSource = self;
        self.tabView.delegate = self;
        [self initData];
    }
    return self;
}

-(void) initData{
    self.arrayData = [NSArray<CellEnity*> new];
    self.topArray = [NSMutableArray<UIImage *> new];
    UIImage *topImage = [UIImage imageNamed:@"noLogin_renwu"];
    UIImage *image = [UIImage imageNamed:@"coupon_bg"];
    NSMutableArray<CellEnity*> *tmpArray = [NSMutableArray<CellEnity*> new];
    for (int i = 0; i<10; i++) {
        [self.topArray addObject:topImage];
        CellEnity * enity = [CellEnity new];
        enity.avatar = image;
        enity.name = [NSString stringWithFormat:@"yuzhuo%d",i+1];
        enity.job = [NSString stringWithFormat:@"射鸡湿%d",i+1];
        enity.content = [NSString stringWithFormat:@"123%d",i+11];
        if(i%2 == 0){
            enity.isParse = YES;
        }else{
            enity.isParse = NO;
        }
        [tmpArray addObject:enity];
    }
    self.arrayData = tmpArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"图享";
    
    self.view.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.tabView];
    self.tabView.frame = self.view.bounds;
//    self.tabView.backgroundColor = [UIColor whiteColor];
    [self.tabView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight ];
    self.tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //三 UINavigationItem 导航元素
    //导航元素属于MVC的M层，作用给当前视图控制器所属的导航控制器的导航栏，提供数据。
    //导航元素 有三种 :leftItem  title rightItem
    //左导航按钮
    //1.创建左导航按钮
    UIImage *leftImage = [[UIImage imageNamed:@"tpo_tab_course"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal] ;
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithImage:leftImage style:(UIBarButtonItemStylePlain) target:self action:@selector(leftAction:)];
    self.navigationItem.leftBarButtonItem = leftButton;
    
    //2右导航按钮 使用图片创建
    UIImage *rightImage = [[UIImage imageNamed:@"community_camera"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal] ;
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithImage:rightImage style:(UIBarButtonItemStylePlain) target:self action:@selector(rightAction:)];
    //注意：iOS 7.0之后，系统默认使用蓝色的tintColor对图片进行渲染，如果需要使用图片原有样式，需要设置图片的渲染模式，为原始状态

    self.navigationItem.rightBarButtonItem = rightButton;

    // Do any additional setup after loading the view, typically from a nib.
    
}

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return self.arrayData.count;
    
}

-(UIView*) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *header = nil;
    if(section == 0){
        header  = [self getHeaderForSectionView];
    }else if(section == 1){
        header = [self getHeaderForCellView];
    }
    return header;
}

-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section <= 1){
        return 50.0f;
    }
    return 0.001f;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 0.001f;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0){
        return 140;
    }else{
        return 200;
    }
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    if(indexPath.section == 0){
        static NSString *cellID = @"topcellID";
        HomeGraphTopCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if(cell == nil){
            cell = [[HomeGraphTopCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }

        cell.data = self.topArray;

        return cell;
    }else{
        static NSString *cellID = @"cellID";
        HomeGraphCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if(cell == nil){
            cell = [[HomeGraphCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        if(indexPath.section < self.arrayData.count+1){
            cell.data = self.arrayData[indexPath.section];
        }
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    if(indexPath.section>0){
        DetailsController *detailview = [DetailsController new];
        [self.navigationController pushViewController:detailview animated:YES];
    }
    NSLog(@"click row%ld",indexPath.row);

}

#pragma mark leftAction
- (void)leftAction:(UIBarButtonItem *)barButton
{
    CellDetailsViewController *detailview = [CellDetailsViewController new];
    
    [self.navigationController pushViewController:detailview animated:YES];
    
}

#pragma mark right
- (void)rightAction:(UIBarButtonItem *)barButton
{
//    CommenFuctionViewController *detailview = [CommenFuctionViewController new];
//    
//    [self.navigationController pushViewController:detailview animated:YES];
    [self getImageFromLocal];
}

-(UIView *)getHeaderForCellView{
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    header.backgroundColor = [UIColor whiteColor];
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 15, SCREEN_WIDTH-30, 20)];
    titleLabel.text = @"每日推荐";
    titleLabel.font = [UIFont systemFontOfSize:18];
    titleLabel.textColor  = [UIColor colorWithHexString:@"#4A4A4A"];
    
    [header addSubview:titleLabel];
    return header;
}

-(UIView *)getHeaderForSectionView{
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    header.backgroundColor = [UIColor whiteColor];
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 15, 100, 20)];
    titleLabel.text = @"精选";
    titleLabel.font = [UIFont systemFontOfSize:18];
    titleLabel.textColor  = [UIColor colorWithHexString:@"#4A4A4A"];
    
    UIButton *checkAllButton  = [UIButton buttonWithType:UIButtonTypeCustom];
    checkAllButton.frame = CGRectMake(SCREEN_WIDTH-100, 0, 90, 50);
    [checkAllButton setTitleColor:[UIColor colorWithHexString:@"#4A4A4A"] forState:UIControlStateNormal];
    checkAllButton.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:15];
    [checkAllButton setTitle:@"查看更多 " forState:UIControlStateNormal];
    [checkAllButton setImage:[UIImage imageNamed:@"arrow"] forState:UIControlStateNormal];
    checkAllButton.titleEdgeInsets = UIEdgeInsetsMake(0, -checkAllButton.imageView.frame.size.width, 0, checkAllButton.imageView.frame.size.width);
    checkAllButton.imageEdgeInsets = UIEdgeInsetsMake(0, checkAllButton.titleLabel.frame.size.width, 0, -checkAllButton.titleLabel.frame.size.width);
    [checkAllButton addTarget:self action:@selector(clickMoreButton:) forControlEvents:UIControlEventTouchUpInside];
    
    
    [header addSubview:titleLabel];
    [header addSubview:checkAllButton];
    return header;
}

-(void)clickMoreButton:(UIButton *)sender{
    
    sender.enabled = NO;
    PickupController *detailview = [PickupController new];
    detailview.titleName = @"精选";
    [self.navigationController pushViewController:detailview animated:YES];
    sender.enabled = YES;
}

- (void)getImageFromLocal
{
  
    [self fetchPictureFromSystem:UIImagePickerControllerSourceTypePhotoLibrary];
    
}

-(void) fetchPictureFromSystem:(NSInteger)sourceType{
    // 跳转到相机或相册页面
    if(imagePickerController==nil){
        imagePickerController = [[UIImagePickerController alloc] init];
    }
    imagePickerController.sourceType = sourceType;
    [self presentViewController:imagePickerController animated:YES completion:^{
        //设置代理
        imagePickerController.delegate = self;
    }];
    
}

#pragma mark - UIImagePickerControllerDelegate

/**
 *  选择完毕执行的方法
 *
 *  @param picker 模态出的控制器
 *  @param info   含有图片信息的字典
 */
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    //dismiss
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    //取出选中的图片
    UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
    //    UIImage* image= [self fixOrientation:[info objectForKey:UIImagePickerControllerOriginalImage]];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
